GoLang

Explain Blank Identifier(Underscore) in Go Programming. | Go Programming Tutorial

Explain Blank Identifier(Underscore) in Go Programming. | Go Programming Tutorial

Explain Blank Identifier(Underscore) in Go Programming. | Go Programming Tutorial

In this article, you will learn about the blank identifier in the Go Programming Language.

What is a Blank Identifier(_) in GoLang?

_ (underscore) in Go Language is called the Blank identifier. Identifiers are user-defined names of the program components that are mainly used for identification purposes. Go Language has a unique feature to define and use the blank identifier as unused variables. Unused variables are variables that are used throughout the program but the user is not able to use these kinds of variables in the program. As we are aware, GoLang is a user-friendly and readable programming language so it does not permit the user to define the unused variables id user attempts to do such, then the compiler will give an error.

The blank identifier comes in use when a function returns multiple values, where it wants to use only a few values and wants to discard some values. Basically, it tells the compiler that the particular variable is not required and ignores it without any error. It hides the values of the variable and makes the program readable. If the user is assigning a value to a Blank identifier it becomes unusable.

Example 1: 

package main
import "fmt"
 
// Main function
func main() {
 
    multiply, _ := multiply_div(105, 7)
 
    // only using the mul variable
    fmt.Println("205 x 15 = ", multiply)
}
 
func multiply_div(num1 int, num2 int) (int, int) {
 
    // returning the values
    return num1 * num2, num1 / num2
}

OUTPUT:

205 x 15 = 3075

Points to be Remembered:

  • The user is allowed to create multiple blank identifiers in the same program. In simple language, a GoLang program can have multiple variables using the same identifiers.
  • The user can use any value of any type with the blank identifier.
  • Various cases arise from the requirement of assignment of values just to complete the syntax even knowing that the values can never be used in the program anywhere.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.